import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * </p>
 *
 * @author zhuolin
 * @since 2020/6/3
 */
public class CountDownLatchDemo implements Runnable {

    static CountDownLatch end = new CountDownLatch(10);


    static int rank;

    String name;

    public CountDownLatchDemo(String name) {
        this.name = name;
    }

    private synchronized int getRank() {
        return rank++;
    }

    @Override
    public void run() {
        try {
            Thread.sleep(new Random().nextInt(10) * 1000);
            end.countDown();
            System.out.println(name + "跑完了");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        ExecutorService es = Executors.newFixedThreadPool(10);
        System.out.println("十名运动员赛跑");
        for (int j = 0; j < 10; j++) {
            es.submit(new CountDownLatchDemo("运动员" + j));
        }
        end.await();
        System.out.println("比赛结束");
        es.shutdown();
    }
}
